package com.chanjiedata.changbs.module.mine;

/**
 * 文件名 : SettingActivity
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/26 16:49
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chanjiedata.changbs.R;
import com.chanjiedata.changbs.base.BaseActivity;
import com.chanjiedata.changbs.base.MainApplication;
import com.chanjiedata.changbs.help.UserHelper;
import com.chanjiedata.changbs.module.login.ForgetPasswordActivity;
import com.chanjiedata.changbs.module.login.LoginActivity;
import com.chanjiedata.changbs.net.Apis;
import com.chanjiedata.changbs.net.NetworkManager;
import com.chanjiedata.changbs.net.WebApis;
import com.chanjiedata.changbs.net.interfaces.OnRequestCallBack;
import com.chanjiedata.changbs.proto.LoginOuterClass;
import com.chanjiedata.changbs.utlis.APKVersionCodeUtils;
import com.chanjiedata.changbs.utlis.CacheUtil;
import com.chanjiedata.changbs.utlis.DialogUtils;
import com.chanjiedata.changbs.utlis.StatusBarUtil;
import com.chanjiedata.changbs.web.ui.WebViewActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SettingActivity extends BaseActivity {

    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.cache_size_tv)
    TextView cacheSizeTv;
    @BindView(R.id.clear_cache_rlt)
    LinearLayout clearCacheRlt;
    @BindView(R.id.login_out_tv)
    Button loginOutTv;
    @BindView(R.id.version_tv)
    TextView versionTv;
    private boolean isEditSuccess;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        StatusBarUtil.setRootViewFitsSystemWindows(this, false);
        StatusBarUtil.setTranslucentStatus(this);
        cacheSizeTv.setText(CacheUtil.getCacheSize(mContext));
        versionTv.setText(APKVersionCodeUtils.getVerName(mContext));
    }

    @OnClick({R.id.back_iv, R.id.person_info_tv, R.id.login_password_tv, R.id.change_phone_tv,
            R.id.clear_cache_rlt, R.id.login_out_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                close();
                break;
            case R.id.person_info_tv: // 安全设置
                Intent intent = new Intent(mContext, ForgetPasswordActivity.class);
                intent.putExtra("title", "修改密码");
                startActivity(intent);
                break;
            case R.id.login_password_tv:  // 支付设置
                Intent intent1 = new Intent(mContext, PayGetCodeActivity.class);
                intent1.putExtra("captcha_type", "7");
                startActivity(intent1);
                break;
            case R.id.change_phone_tv: // 反馈
                startActivity(FeedBackActivity.class);
                break;
            case R.id.clear_cache_rlt:
                DialogUtils.showDefaultDialog(mContext, "", "确定清除缓存？", new DialogUtils.DialogImpl() {
                    @Override
                    public void onOk() {
                        CacheUtil.cleanAllCache(mContext);
                        showToast("清除完毕");
                        cacheSizeTv.setText(CacheUtil.getCacheSize(mContext));
                    }
                });
                break;
            case R.id.login_out_tv:
                DialogUtils.showDefaultDialog(mContext, "", "确定退出登录?", new DialogUtils.DialogImpl() {
                    @Override
                    public void onOk() {
                        NetworkManager.INSTANCE.post(Apis.logout, new OnRequestCallBack() {
                            @Override
                            public void onOk(byte[] bytes){
                            }
                            @Override
                            public void onError(int code, String errorMessage) {
                            }
                        });
                        Intent intent2 = new Intent(mContext, LoginActivity.class);
                        intent2.putExtra("phone", UserHelper.getUser().getMember_mobile());
                        UserHelper.loginOut();
                        startActivity(intent2);
                        MainApplication.finishAllTempActivity();
                        finish();
                    }
                });
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            close();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void close() {
        if (isEditSuccess) setResult(RESULT_OK);
        finish();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && requestCode == 100) {
            isEditSuccess = true;
        }
    }
}
